package ch06.d1;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class DateTest {
    @Test
    public void testNew(){
        //当前
        Date d1=new Date();
        System.out.println(d1);

        Date d2=new Date(951633423324L);
        System.out.println(d2);

    }

    //计算时间差
    @Test
    public void testFormat() throws ParseException {
        String s="2000-2-29 6:30:30";
        //日期解析类
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d=sdf.parse(s);
        //把字符串转换成日期对象

        //计算时间差
        Date now=new Date();
        long c= now.getTime()-d.getTime();

        System.out.println(c/1000/60/60/24/365.0);
    }

    //通过时间差计算新的日期
    @Test
    public void testFormat2() throws ParseException {
        String s = "2000-2-29 6:30:30";
        //日期解析类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = sdf.parse(s);

        long n=d.getTime()+100*24*3600*1000L;
        Date d2=new Date(n);
        System.out.println(d2);

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
        String s2=sdf2.format(d2);// 格式化 把日期对象转换成字符串
        System.out.println(s2);



    }
}
